package nina.yiyou.performance.s20confirm.input

import nina.yiyou.salary.input.util.CellValueGetter
import nina.yiyou.salary.input.util.ExcelReadOpener
import nina.yiyou.salary.input.util.RowGroupValueGetter
import org.apache.poi.xssf.usermodel.XSSFRow
import org.apache.poi.xssf.usermodel.XSSFSheet
import org.apache.poi.xssf.usermodel.XSSFWorkbook

class PerformanceExcelReader {

  PerformanceExcelReader(String excelPath) {
    _excelPath = excelPath
  }

  Map read() {
    XSSFWorkbook workbook = new ExcelReadOpener(_excelPath).openXlsx()
    XSSFSheet sheet = workbook.getSheetAt(0)

    Map result = (2..sheet.lastRowNum).withIndex()
        .findAll { v, i -> i % GROUP_SIZE == 0 }
        .collect { v, i -> v as int }
        .collect { row -> (0..<GROUP_SIZE).collect { sheet.getRow(row + it) } }
        .collect { combine(it) }
        .collectEntries { [it['工号'], it] }

    workbook.close()
    return result
  }

  private Map<String, ?> combine(List<XSSFRow> group) {
    def getValue = { int col -> new RowGroupValueGetter(group, col).getValue() }
    def colN = { int row -> new CellValueGetter(group[row].getCell(13)).get() }
    return [
        '工号': getValue(0),
        '姓名': getValue(1),
        '业绩': colN(0),
        '能力': colN(1),
        '态度': colN(2),
        '考勤': getValue(15),
        '自评': getValue(19),
        '他评': getValue(20),
    ]
  }

  private static final int GROUP_SIZE = 3

  private final String _excelPath
}
